<?php

class Payment extends Web_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Order_model', 'order_model');
        $this->load->helper('redis');
    }

    // 威富通/微信扫码支付(提交订单)
    public function wxinpay()
    {
        $user = $this->userSession();
        $openid = $this->session->openid;
        if (empty($user->uid)) {
            $base_url = $this->config->item('base_url');
            header("Location: {$base_url}home/account/login");
            return;  
        }
        $orderid = $this->input->post('order_id');
        $paytype = 1;//微信扫码支付
        if (empty($orderid) || ! ctype_digit($orderid)) {
            //参数错误
            echo json_encode(array('errcode' => 1, 'errmsg' => '参数错误'));
            return;
        }

        ///根据orderid从redis读取订单数据
        $orderData = json_decode(getOrderFromRedisByOrderid($orderid));

        if (!empty($orderData)) {
            $data = array(
                "out_trade_no" => $orderid.'_'.$paytype,
                "body" => '养悦源0元购',
                //"openid" => "",
                "paytype" => $paytype,
                "is_raw" => 1,
                "method" => "submitOrderInfo",
                // "amount" => ctype_digit($orderid) ? 2 : '1',
                "mch_create_ip" => $_SERVER["REMOTE_ADDR"],
                "total_fee" => (int)($orderData->order->price * 100),
                "time_start" => date('YmdHis', $orderData->order->create_time),
                "time_expire" => date('YmdHis', $orderData->order->create_time + 30*60),//30分钟后超时
            );

            $this->load->library('Swiftpass', 'swiftpass');
            $pay_info = $this->swiftpass->submitOrderInfo($data);
        } else {
            //订单不存在
            echo json_encode(array('errcode' => 2, 'errmsg' => '订单不存在'));
        }

    }

    //微信公众号支付（威富通）
     public function wxinoppay()
    {   

        $user = $this->userSession();
        $openid = $this->session->openid;
        if (empty($user->uid)) {
            $base_url = $this->config->item('base_url');
            header("Location: {$base_url}home/account/login");
            return;  
        }
        $orderid = $this->input->post('order_id');
        $paytype = 2;//微信公众号支付

        if (empty($orderid) || ! ctype_digit($orderid)) {
            echo json_encode(array('errcode' => 1, 'errmsg' => '参数错误'));
            return;
        }

        if(empty($openid))
        {
            echo json_encode(array('errcode' => 3, 'errmsg' => '请从微信客户端进入'));
            return;
        }

        //根据orderid从redis读取订单数据
        $orderData = json_decode(getOrderFromRedisByOrderid($orderid));
        if (!empty($orderData)) {
            $data = array(
                "out_trade_no" => $orderid.'_'.$paytype,
                "body" => '养悦源0元购',
                "sub_openid" => $openid,
                "paytype" => $paytype,
                "is_raw" => 1,
                "method" => "submitOrderOpInfo",
                "mch_create_ip" => $_SERVER["REMOTE_ADDR"],
                "total_fee" => (int)($orderData->order->price * 100),
                "time_start" => date('YmdHis', $orderData->order->create_time),
                "time_expire" => date('YmdHis', $orderData->order->create_time + 30*60),//30分钟后超时
            );
            $this->load->library('Swiftpass', 'swiftpass');
            $this->swiftpass->submitOrderOpInfo($data);
        } else {
            //订单不存在
            echo json_encode(array('errcode' => 2, 'errmsg' => '订单不存在'));
        }
    }

    // 威富通/微信公众号支付(提交订单)
    public function zhifbscpay()
    {
        $user = $this->userSession();
        if (empty($user->uid)) {
            $base_url = $this->config->item('base_url');
            header("Location: {$base_url}home/account/login");
            return;
        }
        $orderid = $this->input->post('order_id');
        $paytype = 3;//支付宝扫码支付
        if (empty($orderid) || ! ctype_digit($orderid)) {
            //参数错误
            echo json_encode(array('errcode' => 1, 'errmsg' => '参数错误'));
            return;
        }

        //根据orderid从redis读取订单数据
        $orderData = json_decode(getOrderFromRedisByOrderid($orderid));

        if (!empty($orderData)) {
            $data = array(
                "out_trade_no" => $orderid.'_'.$paytype,
                "body" => '养悦源0元购',
                "paytype" => $paytype,
                "is_raw" => 1,
                "method" => "submitOrderInfozfb",
                "mch_create_ip" => $_SERVER["REMOTE_ADDR"],
                "total_fee" => (int)($orderData->order->price * 100),
                "time_start" => date('YmdHis', $orderData->order->create_time),
                "time_expire" => date('YmdHis', $orderData->order->create_time + 30*60),//30分钟后超时
            );

            $this->load->library('Swiftpass', 'swiftpass');
            $pay_info = $this->swiftpass->submitOrderInfozfb($data);
        } else {
            //订单不存在
            echo json_encode(array('errcode' => 2, 'errmsg' => '订单不存在'));
        }

    }
}
